﻿using System;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.BI11.Studio.InternalServices;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.SituationsHierarchy
{
    /// <summary>
    /// Диалог настройки значение неопределенности / переключения характеристики на обязательную
    /// </summary>
    public partial class UncertaintyValueDialog : RestorableForm
    {
        private Characteristic _characteristic; //характеристика, у которой настраиваем значение неопределенности

        /// <summary>
        /// Инициализация компонентов
        /// </summary>
        public UncertaintyValueDialog(Characteristic characteristic)
        {
            _characteristic = characteristic;
            InitializeComponent();            
        }

        /// <summary>
        /// Обновление данных о доступных значениях
        /// </summary>
        private void UpdateItemsList()
        {
            ItemsListBox.Items.Clear();
            var ind = _characteristic.UsedIndicator;
            IndicatorLimiter limiter = _characteristic.IndicatorLimiter;
            if (limiter != null)
            {
                ind = limiter.InternalIndicator;                
            }
            int c = ind.GetIndicatorValuesCount();
            for (int i = 0; i < c; i++)
            {
                string s = "";
                var iv = ind.GetIndicatorValueByPosition(i);
                object obj = iv.Value;
                if (obj != null)
                {
                    s = obj.ToString();
                    ItemsListBox.Items.Add(s);
                }
                else
                {
                    ItemsListBox.Items.Add(Constants.NullDoesNotMatterString);
                }
            }        
        }

        /// <summary>
        /// Выбирает значение в качестве значение неопределенности
        /// </summary>
        /// <param name="position"></param>        
        private void UpdateItem(int position)
        {
            var ind = _characteristic.UsedIndicator;
            IndicatorLimiter limiter = _characteristic.IndicatorLimiter;
            if (limiter != null)
            {
                ind = limiter.InternalIndicator;
            }
            try
            {
                _characteristic.SetUncertaintyValue(ind.GetIndicatorValueByPosition(position).Value);
            }
            catch (BISituationsHierarchyException ex)
            {
                GeneralMethods.ShowUserError(ex.Message);
                InitializeList();
            }
        }

        /// <summary>
        /// Стартовая подготовка списка
        /// </summary>
        private void InitializeList()
        {
            UpdateItemsList();
            var ind = _characteristic.UsedIndicator;
            IndicatorLimiter limiter = _characteristic.IndicatorLimiter;
            if (limiter != null)
            {
                ind = limiter.InternalIndicator;
            }
            int index = ind.GetPositionOfIndicatorValue(_characteristic.UncertaintyValue);
            ItemsListBox.SelectedIndex = index;            
        }

        private void ItemsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateItem(ItemsListBox.SelectedIndex);
        }

        private void UncertaintyValueDialog_Load(object sender, EventArgs e)
        {
            InitializeList();            
        }
    }
}
